<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link href="../css/icons-extra.css" rel="stylesheet"/>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">	
			<a class="mui-icon mui-icon-back mui-action-back mui-pull-left"></a>
			<h1 class="mui-title ">Accelerometer</h1>
			<a class="mui-icon-extra mui-icon-extra-topic mui-pull-right" style="margin-top: 10px;"
				 onclick="openWindow('accelerometer','../doc/accelerometer.html?v=20190621')"></a>
		</header>
		<div class="mui-content">
			<div class="mui-content-padded">
				<div class="mui-row">
					Accelerometer模块管理设备加速度传感器，用于获取设备加速度信息，包括x（屏幕水平方向）、y（垂直屏幕水平方向）、z（垂直屏幕平面方向）三个方向的加速度信息。通过plus.accelerometer获取设备加速度传感器管理对象。
				</div>
				
				<div class="mui-button-row">
					<button type="button" class="mui-btn mui-btn-primary mui-btn-block" onclick="openWindow('accelerometer_shake','accelerometer_shake.html?v=20190621')">摇一摇</button>
					<button type="button" class="mui-btn mui-btn-success mui-btn-block" onclick="getAcc();">获取设备当前加速度</button>
					<button type="button" class="mui-btn mui-btn-danger mui-btn-block mui-btn-outlined" onclick="watchAcc();">监听设备加速度变化</button>
					<button type="button" class="mui-btn mui-btn-royal mui-btn-block mui-btn-outlined" onclick="watchStop();">停止监听设备加速度变化</button>
				</div>
				<br />
				<div class="mui-row" id="divInfo"></div>
			</div>
		</div>
		<script src="../js/mui.min.js"></script>
		<script src="../js/app.js"></script>
		<script type="text/javascript">
			mui.init()
			
			function getAcc() {
				outSet('获取设备当前加速度');
				plus.accelerometer.getCurrentAcceleration(function(a){
					outLine('X轴：'+a.xAxis+'\nY轴：'+a.yAxis+'\nZ轴：'+a.zAxis);
				}, function(e){
					outLine('获取失败:'+e.message);
				} );
			}
			var aid = null;
			function watchAcc(){
				if(aid){
					return;
				}
				outSet('监听设备加速度变化');
				aid = plus.accelerometer.watchAcceleration(function(a){
					outSet('监听设备加速度变化\n'+'X轴：'+a.xAxis+'\nY轴：'+a.yAxis+'\nZ轴：'+a.zAxis);
				}, function(e){
					outLine('监听失败:'+e.message);
				});
			}
			function watchStop() {
				if(aid){
					outSet('停止监听设备加速度变化');
					plus.accelerometer.clearWatch(aid);
					aid = null;
				}else{
					outSet('没有监听设备加速度变化');
				}
			}
			
			function outSet(message){
				document.getElementById("divInfo").innerText = message;
			}
			
			function outLine(message){
				var text = document.getElementById("divInfo").innerText;
				document.getElementById("divInfo").innerText = text + "\n" + message;
			}
		</script>
	</body>

</html>